import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/app/modules/state/getbuilder/counter_controller.dart';

class GetBuilderPage extends GetView {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('GetBuilderPage'),
        centerTitle: true,
      ),

      body: SafeArea(
        child: ListView(
          children: [
            Text(
              '- GetBuilder 需要手动刷新\n- 通过 Id 做精细化的控制\n- 可以使用普通变量',
              style: Theme.of(context).textTheme.headline5
            ),
            Divider(),

            GetBuilder<CounterController>(
              init: CounterController(),
              initState: (state) {
                state.controller?.x = 20;
              },
              builder: (controller) {
                print('x rebuild');
                return Text(
                  'X: '+controller.x.toString(),
                  style: Theme.of(context).textTheme.headline4
                );
              }
            ),
            GetBuilder<CounterController>(
              // init: CounterController(), // 第二次调用 GetX 时，对象已经初始化
              builder: (controller) {
                print('y rebuild');
                return Text(
                  'Y: '+controller.y.toString(),
                  style: Theme.of(context).textTheme.headline4
                );
              }
            ),

            GetBuilder<CounterController>(
              builder: (_) {
                print('sum rebuild');
                return Column(
                  children: [
                    Text(
                      'X+Y: '+_.sum.toString(),
                      style: Theme.of(context).textTheme.headline4
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            _.incrementX();
                            // 手动更新
                            _.update();
                          }, 
                          child: Text('X +')
                        ),
                        SizedBox(width: 10,),
                        ElevatedButton(
                          onPressed: () {
                            _.incrementY();
                          }, 
                          child: Text('Y +')
                        ),
                      ],
                    )
                  ],
                );
                
              }
            ),
            Divider(),

            GetBuilder<CounterController>(
              id: "pager",
              init: CounterController(),
              builder: (_) {
                print('y rebuild again');
                return Column(
                  children: [
                    Text(
                      'Y: '+_.y.toString(),
                      style: Theme.of(context).textTheme.headline4
                    ),
                    ElevatedButton(
                      onPressed: () {
                        _.incrementY();
                        _.update(['pager']);
                      }, 
                      child: Text('Y +')
                    )
                  ],
                );
              }
            ),
          ]
        )
      )
    );
  }
}